package controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import services.auto.AutoService;
import beans.AutoVO;

@Controller
public class SeleccionAutoController {

	@Autowired
	private AutoService autoService;
	
	@RequestMapping(value="public/buscarAuto", method = RequestMethod.GET)
	public ModelAndView alta(@ModelAttribute AutoVO autoNuevo){
		ModelAndView mv = new ModelAndView();
		List<AutoVO> listaAutos;
		try {
			if(!autoNuevo.getPatente().equals("")) {
				listaAutos = new ArrayList<AutoVO>();
				listaAutos.add(autoService.selectByPatente(autoNuevo));
			}else{
				listaAutos = autoService.selectAll();
			}
			mv.addObject("listaAutos", listaAutos);
			mv.setViewName("buscar");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}

	public void setAutoService(AutoService autoService) {
		this.autoService = autoService;
	}
}
